package com.skivingcloud.admin.sys.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.security.SecurityUser;
import com.skivingcloud.admin.sys.entity.SysDictionary;
import com.skivingcloud.admin.sys.mapper.SysDictionaryMapper;
import com.skivingcloud.admin.sys.service.SysDictionaryService;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 *  服务层实现。
 *
 * @author skivingcloud
 * @since 2023-08-28
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements SysDictionaryService {
    
    /**
     * 根据字典编号取字典数据
     *
     * @param classCode 字典编码
     * @return 字典项
     */
    @Override
    @Cacheable(value="skivingDic",key="#p0 + '_queryByClassCode'")
    public List<SysDictionary> queryByClassCode(String classCode){
        QueryWrapper queryWrapper = QueryWrapper.create().where(SysDictionary::getClassCode).eq(classCode)
                .orderBy(SysDictionary::getDataOrder).asc();
        return this.list(queryWrapper);
    }
    
    /**
     * 新增数据字典
     *
     * @param sysDictionary 数据字典
     * @param currentUser   操作人
     */
    @Override
    @CacheEvict(value="skivingDic",key="#p0.classCode + '_queryByClassCode'")
    public void addDic(SysDictionary sysDictionary, SecurityUser currentUser){
        sysDictionary.setId(UuidUtil.get32UUID());
        this.mapper.insert(sysDictionary);
    }
    
    /**
     * 修改数据字典
     *
     * @param sysDictionary 数据字典
     * @param currentUser   操作人
     */
    @Override
    @CacheEvict(value="skivingDic",key="#p0.classCode + '_queryByClassCode'")
    public void updateDic(SysDictionary sysDictionary, SecurityUser currentUser){
        this.mapper.update(sysDictionary);
    }
    
    /**
     * 删除数据字典
     *
     * @param id 字典id
     */
    @Override
    @CacheEvict(value="skivingDic", allEntries = true)
    public void deleteDic(String id){
        this.mapper.deleteById(id);
    }
    
    /**
     * 分页查询数据字典
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysDictionary> queryByPage(PageParameter<SysDictionary> parameter){
        Page<SysDictionary> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        SysDictionary sysDictionary = parameter.getParam();
        QueryWrapper queryWrapper = QueryWrapper.create().where(" 1=1 ");
        if(sysDictionary != null){
            if(StringUtils.isNotBlank(sysDictionary.getClassCode())){
                queryWrapper.and(SysDictionary::getClassCode).like(sysDictionary.getClassCode());
            }
            if(StringUtils.isNotBlank(sysDictionary.getClassName())){
                queryWrapper.and(SysDictionary::getClassName).like(sysDictionary.getClassName());
            }
            if(StringUtils.isNotBlank(sysDictionary.getDataCode())){
                queryWrapper.and(SysDictionary::getDataCode).like(sysDictionary.getDataCode());
            }
            if(StringUtils.isNotBlank(sysDictionary.getDataName())){
                queryWrapper.and(SysDictionary::getDataName).like(sysDictionary.getDataName());
            }
            if(StringUtils.isNotBlank(sysDictionary.getState())){
                queryWrapper.and(SysDictionary::getState).eq(sysDictionary.getState());
            }
        }
        queryWrapper.orderBy(" class_code,  data_order");
        return this.page(page, queryWrapper);
    }
    
    /**
     * 批量删除
     *
     * @param ids 主键
     */
    @Override
    @CacheEvict(value="skivingDic", allEntries = true)
    public void deleteBatch(String[] ids){
        this.mapper.deleteBatchByIds(Arrays.stream(ids).toList());
    }
    
    /**
     * 根据主键获取详细信息。含有上级名称
     *
     * @param id 主键
     * @return 字典信息
     */
    @Override
    public SysDictionary getInfoByIdHasParent(Serializable id){
        SysDictionary sysDictionary = this.getById(id);
        if (StringUtils.isNotBlank(sysDictionary.getParentId())) {
            SysDictionary parent = this.getById(sysDictionary.getParentId());
            if (parent != null) {
                sysDictionary.setParentName(parent.getClassName() + "/" + parent.getDataName());
            }
        }
        return sysDictionary;
    }
}
